﻿@{
    ViewBag.Title = "ShopCar";
    Layout = "~/Views/Users/Index.cshtml";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>

<link href="~/css/bootstrap.min.css" rel="stylesheet" />

<script src="~/js/bootstrap.min.js"></script>
<script src="~/Scripts/layer/layer.js"></script>
<h2>ShopCar</h2>

<table class="table table-hover">
    <tr>
        <th>商品</th>
        <th>花卉名称</th>
        <th>购买数量</th>
        <th>单价</th>
        <th>总金额</th>
        <th>添加时间</th>
        <th>操作</th>

    </tr>
    @foreach (var i in Model)
    {
        <tr style="line-height:60px">
            <td><img src="@i.FlowerUrl" style='width:60px;height:70px;' /><hidden></hidden></td>
            <td style="line-height:60px"><input id="Checkbox1" type="checkbox" onchange="totals()" name="cks" value="@i.ShopID" />@i.FlowersName</td>
            <td style="line-height:60px"><a href="#" onclick="subtract(this,@i.ShopID,@i.Sales_unit_price)">-</a>  <input id="@i.ShopID" type="text" disabled="disabled" style="width:30px" value="@i.purchaseNumber" />  <a href="#" onclick="Add(this,@i.ShopID,@i.Sales_unit_price)">+</a></td>
            <td style="line-height:60px">@i.Sales_unit_price</td>
            <td style="line-height:60px">@(i.purchaseNumber * i.Sales_unit_price)</td>
            <td style="line-height:60px">@i.Place_Time</td>
            <td style="line-height:60px"><a href="/FlowersStrock/deleteF/@i.ShopID">删除</a></td>
        </tr>
    }
</table>
<span>总金额：</span><span id="sp">0</span>
<input id="Button1" type="button" value="下订单" onclick="order()" class="btn btn-primary btn-sm" disabled="disabled" />
<script>
    //减数量
    function subtract(th,i,price)
    {
        //数量获取到并减一
        if($("#"+i+"").val()>1)
        {
            $("#"+i+"").val(parseInt($("#"+i+"").val()) - 1);
        }
        else
        {
            return;
        }
        var sums = $("#"+i+"").val();
        var dd=$(th).parent().parent().children().eq(4);
        $.ajax({
            url: "/FlowersStrock/update",
            dataType: "json",
            type: "post",
            data:{sums:sums,shopid:i},
            success: function (obj) {
                if(obj>0)
                {
                    dd.text(parseInt(sums)*price);
                }
                else
                {
                    $("#"+i+"").val(parseInt(sums)+1);
                }

            }
        })
    }
    //加数量
    function Add(th,i,price)
    {
        var count=0;
        $.ajax({
            url:"/Flowers/getCounts?id="+i,
            dataType:"json",
            type:"post",
            success:function(obj){
                count=obj;

                if($("#"+i+"").val()<count)
                {
                    $("#"+i+"").val(parseInt($("#"+i+"").val()) + 1);
                }
                else
                {
                    return;
                }
                var sums = $("#"+i+"").val();
                var dd=$(th).parent().parent().children().eq(4);
                $.ajax({
                    url: "/FlowersStrock/update",
                    dataType: "json",
                    type: "post",
                    data:{sums:sums,shopid:i},
                    success: function (obj) {
                        if(obj>0)
                        {
                            dd.text(parseInt(sums)*price);
                        }
                        else
                        {
                            $("#"+i+"").val(parseInt(sums)-1);
                        }

                    }
                })
            }
        })
    }
    //总金额
    function totals()
    {
        var total = 0;
        //根据复选框的选中状态进行统计总价
        if ($("[name='cks']:checked").length != 0)
        {
            $("[name='cks']:checked").each(function () {
                total = parseInt(total) + parseInt($(this).parent().parent().children().eq(4).text());
            });
        }
        else
        {
            total = 0;
        }
        $("#sp").text(total);
        if($("#sp").text()!=0)
        {
            $("#Button1").attr("disabled", false);
        }else
        {
            $("#Button1").attr("disabled", true);
        }
    }
    //下单
    function order()
    {
        //总金额
        var zong=$("#sp").text();
        //花卉名称的数组
        var name = new Array();
        //商品编号的数组
        var ids=new Array();
        ///价格的数组
        var price = new Array();
        //数量的数组
        var numbers = new Array();
        //总数量的数组
        var total = new Array();
        //地址的数组
        var url=new Array();
        //循环把各个项添加到数组
        $("[name='cks']:checked").each(function () {
            name.push($(this).parent().text());
            price.push($(this).parent().parent().children().eq(3).text());
            numbers.push($("#" + $(this).val() + "").val());
            total.push($(this).parent().parent().children().eq(4).text());
            ids.push($(this).val());
            url.push($(this).parent().parent().children().find("img").attr("src"));
        })
        var html="";
        for(var i=0;i<name.length;i++)
        {
            html+="<tr><td ><img style='width:30px;height:30px;' src='"+url[i]+"'/></td><td>"+ids[i]+"</td><td>"+name[i]+"</td><td>"+price[i]+"</td><td>"+numbers[i]+"</td><td>"+total[i]+"</td></tr>"
        }
        var htmls="<table class='table'><tr><th>图片</th><th>商品编号</th><th>花卉名称</th><th>单价</th><th>数量</th><th>金额</th></tr>";
        htmls+=html;
        htmls+="<tr><td></td><td></td><td></td><td></td><td></td><td><span id='ps'>总计："+zong+"</span></td></tr></table>";
        htmls+="<table class='table'><tr><td style='text-align:right'>收货地址：</td><td><input id='Text1' type='text' name='address'/><span ></span></td></tr>";
        htmls += "<tr><td style='text-align:right'>联系人手机：</td><td><input id='Text1'  type='tel' name='tell' requ/></td></tr>";
        htmls += "<tr><td style='text-align:right'>配送方式：</td><td><input id='Radio1' type='radio' name='method' value='直送'/>直送 <input id='Radio1' type='radio' name='method' value='快递'/>快递</td></tr>";
        htmls += "<tr><td style='text-align:right'>支付方式：</td><td><input id='Radio1' type='radio' name='paymethod' value='在线支付'/>在线支付 <input id='Radio1' type='radio' name='paymethod' value='货到付款'/>货到付款</td></tr>";
        htmls += "<tr><td></td><td><input id='Button1' type='button' value='确认订单' onclick='retry()' class='btn' style='float:right'/></td></tr></table>";
        layer.open({
            type: "1",
            area: ["800px", "400px"],
            title: "订单信息",
            content: htmls
        });
    }
    //确认订单
    function retry()
    {
        ///注释同下单
        var zong = $("#sp").text();
        var name = new Array();
        var ids = new Array();
        var price = new Array();
        var numbers = new Array();
        var total = new Array();
        var url=new Array();
        $("[name='cks']:checked").each(function () {
            name.push($(this).parent().text());
            price.push($(this).parent().parent().children().eq(3).text());
            numbers.push($("#" + $(this).val() + "").val());
            total.push($(this).parent().parent().children().eq(4).text());
            ids.push($(this).val());
            url.push($(this).parent().parent().children().find("img").attr("src"));
        });
        var address = $("[name='address']").val();
        var tell = $("[name='tell']").val();
        var method = $("[name='method']").val();
        var paymethod = $("[name='paymethod']").val();

        $.ajax({
            url: "/Order/AddOrders",
            data: JSON.stringify({name:name,address:address,price:price,numbers:numbers,ids:ids,total:total,tell:tell,method:method,paymethod:paymethod,zong:zong,url:url}),
            type: "post",
            contentType:"application/json",
            success: function (hu) {
                if(hu>0)
                {
                    alert('下单成功');
                    layer.closeAll();
                    $("[name='cks']:checked").each(function () {
                        $(this).parent().parent().remove();
                    })
                }
                else
                {
                    alert('下单失败');
                    layer.closeAll();
                }
            }
        })
    }
    function getcount()
    {
        var count=0;
        $.ajax({
            url:"/Flowers/getCounts?id="+i,
            dataType:"json",
            type:"post",
            success:function(obj){
                count=obj;
            }
        })
    }
</script>